<?php

/**
 * Loging in a CRMUser
 * @package ModuleAdministratif
 * @version 1.0b
 * @var $_POST['login'] CRMUser's login
 * @var $_POST['password'] CRMUser's clear password
 */

session_start();

// Configuration file
include '../configuration.php';
// Functions
include '../inc/functions.php';

// Include all objects
// TODO: Include only needed classes
foreach (glob($configuration['webroot'].'objects/*.php') as $class) { include_once $class; }

try {
	$crmuser = new CRMUser();
	$crmuserList = $crmuser->GetList(Array(Array('login', '=', $_POST['login']), Array('password', '=', UnicrmHash($_POST['password']))));
	
	// Case: login and password correct
	if (count($crmuserList) > 0):
	
		// Loading user environment
		$crmuser = $crmuserList[0];
		$credentials = $crmuser->GetCredentials();
		$_SESSION['current_user'] = serialize($crmuser);
		$_SESSION['current_credentials'] = serialize($credentials);
		
		echo 'OK';
		
	// Case: login and/or password incorrect
	else:
		echo 'Nom d\'utilisateur et/ou mot de passe incorrect(s)';
	endif;
	
} catch (Exception $e) {
	echo $e->getMessage();
}
?>